import re
import h5py
import pandas as pd
import numpy as np
from threading import Timer
import time
from datetime import datetime, timezone, timedelta
import tushare as ts
import os
# 处理数据错误

# f = open('./2017-09-19.csv')
#
# new_lines = []
#
# n = 0
# for line in f:
#     if n/1000 == n//1000:
#         print(n, line)
#         processed_line = re.compile(r'(?!\d+)\B(?=s[z|h])', re.X).subn('\n', line)[0]
#         new_lines.append(processed_line)
#     else:
#         new_lines.append(line)
#     n += 1
#
# f2 = open('test.csv', 'a')
# f2.write(''.join(new_lines))
# f2.close()


# f = h5py.File('2017-09-19.h5', 'a')
#
# dset = f.create_dataset("zipped", (100, 100), compression="gzip")


# dt = pd.read_csv('./tasks/stock_ticker/2017-09-20.csv')
#
# d2 = dt.drop_duplicates()
# d2.to_csv('2017-09-20.csv')

# print('aksdjl;kasjd;adsasd,1505969977441'[0:-14])
#

# print(str(round(time.time() * 1000)))

# timer_interval = 1
#
#
# def delayrun():
#     print('running')
#
# print('run')
# t = Timer(timer_interval, delayrun)
# t.start()
# # while True:
# #     time.sleep(1)
# #     print('main running')


# print(datetime.now().strftime('%Y%m%d') not in ['20170927', '20171002', '20171003', '20171004', '20171005', '20171006'])

# df = ts.get_stock_basics()
# for index, stock in df.iterrows():
#     print(index)

# print(re.compile(r'(var hq_str_)|(\")|;', re.M).subn('', 'var hq_str_=22.22')[0].replace('=', ','))

# list分段
# def splist(l, s):
#     return [l[i:i+s] for i in range(len(l)) if i % s == 0]
#
# print(splist(['a', 'b','c','d','e','f','g'], 3))
#
# # 将列表分段
# def update_code_list():
#     df = ts.get_stock_basics()
#     full_list = list(map(lambda code: str('sh' + code[0:] if code[0] == '6' else 'sz' + code[0:]), list(df.index)))
#     map(lambda a: ','.join(a), splist(full_list, 500))
#     return map(lambda a: ','.join(a), splist(full_list, 500))


# dft = pd.DataFrame(np.random.randn(100000,1), columns=['A'], index=pd.date_range('20130101',periods=100000,freq='T'))
#
# print(dft.index.__class__)




# st = 'sh600354,第一创业,0.000,9.080,9.080,0.000,0.000,0.000,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,2017-09-21,09:10:06,00'
# out = re.compile(',(?=[0-9]{2}:[0-9]{2}:[0-9]{2})').sub(' ', st)
# print(out.split(','))
# reg = re.compile('^(.*,){2}').sub('ww', st)
# print(reg)
# print(float('0.000'))
#
#
# dic = {
#     'name': 'joeslee',
#     'sex': 'male'
# }
#
# for key, val in dic.items():
#     print(key, val)
#
#
# print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(time.mktime(time.strptime("2017-10-01 14:16:00", "%Y-%m-%d %H:%M:%S")))))


# df = ts.get_stock_basics()
#
# df.to_csv('stock-list.csv')

# import os
# path = '../etl/stocks'
# for dirpath, dirnames, filenames in os.walk(path):
#     for file in filenames:
#             fullpath=os.path.join(dirpath, file)
#             print(file.replace('.csv', '')[2:])

tick = 'sz300715,凯伦股份,0.000,31.900,0.000,0.000,0.000,0.000,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,2017-11-06,09:13:57,00'
st = re.compile(',(?=[0-9]{2}:[0-9]{2}:[0-9]{2})').sub(' ', tick)
# print(st)
# print(re.search('[0-9]{4}-3[0-9]{2}-[0-9]{2},[0-9]{2}:[0-9]{2}:[0-9]{2}', tick))
print(re.search('s[hz][0-9]{6}', tick).group())

m = re.search('[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]{2}:[0-9]{2}:[0-9]{2}', tick)
if m:
    g = m.group()
    if isinstance(g, str):
        event_time = g.replace(',', ' ')
        dtime = datetime.strptime(g, '%Y-%m-%d,%H:%M:%S').replace(tzinfo=timezone(timedelta(hours=8)))
        print(time.mktime(dtime.timetuple()))


# for key, tick in {"a": 1, "b": 2}.items():
#     print(key, tick)
#
obj = {1:'q',3:'c',2:'g'}
print(sorted(obj.keys()))
for k in sorted(obj.keys()):
    print(obj[k])

class TimedReciever:
    def __init__(self, flush_method):
        self.flush_method = flush_method
        self.queue = []
        self.timer = None

    def append(self, item):
        self.queue.append(item)
        if self.timer and self.timer.isAlive:
            self.timer.cancel()
        self.timer = Timer(3, self.__flush)
        self.timer.start()

    def __flush(self):
        if self.flush_method(self.queue):
            self.queue.clear()


# def flu(x):
#     print(x)
#     return True
# rec = TimedReciever(flu)
#
# rec.append(1)
# rec.append(2)
# rec.append(2)
# rec.append(2)
#
#
# # os.O_CREAT|os.O_EXCL|os.O_RDWR
# file_name = 'test.csv'
# lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
#
# f = os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
# print(f)

# print(ts.get_stock_basics())

# import http.client
# conn = http.client.HTTPConnection("www.55188.com")
# conn.request("GET", "/viewthread.php?tid=5551406")
# r1 = conn.getresponse()
# print(r1.status, r1.reason)
# data1 = r1.read()
# print(data1)

import urllib.request
import inspect

this_file = inspect.getfile(inspect.currentframe())
dir_path = os.path.abspath(os.path.dirname(this_file))
with urllib.request.urlopen("http://www.55188.com/viewthread.php?tid=5551406") as f:
    st = str(f.read())
    result = re.compile('https?://pan.baidu.com/s/[^"<>]+').search(st)
    if result:
        link = result.group()
        date = datetime.today().strftime('%Y-%m-%d')
        with open(os.path.join(dir_path, 'links.csv'), 'a') as file:
            file.writelines(date + ' ' + link)
            file.close()
